#include <cstring>
#include "../Basics/BasicStruct.cpp"

/* 图
    typedef struct ArcNode{ // 弧边信息
        int from;
        int adj; // 当前弧对应可以到达的顶点， 比如弧为1-4 则to为4
        struct ArcNode *next; // 下一条弧的信息
    }ArcNode;

    typedef struct VertexNode{ // 顶点信息
        int data; // 当前顶点的值
        ArcNode* firstArc; // 所有弧的信息，用链表形式存储。
    }VertexNode;


    typedef struct{
        VertexNode* vertex[10];// 顶点信息,每个顶点信息里存着能到达的其他顶点
        int vexnum,arcnum; // 总顶点个数以及弧边个数.
    }*Graph,G;
 */

//  1 - 2 - 3
//   \ /
//    4

// 根据map文件创建邻接表
Graph CreateGraphByFile(string filename) {
    Graph g = (Graph) malloc(sizeof(G));
    string path = "C:\\Users\\Administrator\\CLionProjects\\RealQuestion\\Map-Question\\";
    string realPath = path+filename;
    FILE* fq=fopen(realPath.c_str() ,"r");
    fscanf(fq,"%d",&g->vexnum);
    for(int i =1;i <= g->vexnum;i++) {
        g->vertex[i] = (VertexNode*) malloc(sizeof(VertexNode));
        g->vertex[i]->firstArc = NULL;
        g->vertex[i]->data = i;
    }

    int from,to;
    int arcnum = 0;
    while(fscanf(fq,"%d %d",&from,&to) != EOF) {
        arcnum++;
        ArcNode* arcNode = (ArcNode*) malloc(sizeof(ArcNode));

        arcNode->next = NULL;

        arcNode->adj = to;

        arcNode->next = g->vertex[from]->firstArc;
        g->vertex[from]->firstArc = arcNode;
    }
    g->arcnum = arcnum;
    fclose(fq);
    return g;
}

GraphMap CreateGraphMapByFile(string filename) {
    GraphMap g = (GraphMap) malloc(sizeof(G2));
    memset(g->map, false,sizeof g->map);
    string path = "C:\\Users\\Administrator\\CLionProjects\\RealQuestion\\Map-Question\\";
    string realPath = path+filename;
    FILE* fq=fopen(realPath.c_str() ,"r");
    fscanf(fq,"%d",&g->vexnum);

    int from,to;
    int arcnum = 0;
    while(fscanf(fq,"%d %d",&from,&to) != EOF) {
        arcnum++;
        g->map[from][to] = true;
    }
    g->arcnum = arcnum;
    fclose(fq);
    return g;
}

int FirstAdjVertex(GraphMap g,int v) {
    for (int i = 1; i <= g->vexnum; ++i) {
        if (g->map[v][i]) {
            return i;
        }
    }
    return -1;
}

int NextAdjVertex(GraphMap g,int v,int w) {
    for (int i = w+1; i <= g->vexnum; ++i) {
        if (g->map[v][i]) {
            return i;
        }
    }
    return -1;
}

void Display(Graph g,int* path) {
    for (int i = 0; i < g->vexnum; ++i) {
        printf("%d ",path[i]);
    }
}

void DisplayMap(GraphMap g,int* path) {
    for (int i = 0; i < g->vexnum; ++i) {
        printf("%d ",path[i]);
    }
}